package cn.stylefeng.guns.sys.modular.plotHouseInfo.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.auth.model.LoginUser;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.personInfo.mapper.PersonInfoMapper;
import cn.stylefeng.guns.sys.modular.personInfo.model.params.PersonInfoParam;
import cn.stylefeng.guns.sys.modular.plotHouseInfo.entity.HouseInfo;
import cn.stylefeng.guns.sys.modular.plotHouseInfo.mapper.HouseInfoMapper;
import cn.stylefeng.guns.sys.modular.plotHouseInfo.model.TreeBean;
import cn.stylefeng.guns.sys.modular.plotHouseInfo.model.params.HouseInfoParam;
import cn.stylefeng.guns.sys.modular.plotHouseInfo.model.result.HouseInfoResult;
import cn.stylefeng.guns.sys.modular.plotHouseInfo.service.HouseInfoService;
import cn.stylefeng.guns.sys.modular.plotHouseInfo.service.plotTree;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.cj.xdevapi.JsonArray;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;

/**
 * <p>
 * 房产表 服务实现类
 * </p>
 *
 * @author WangZhen
 * @since 2021-01-21
 */
@Service
public class HouseInfoServiceImpl extends ServiceImpl<HouseInfoMapper, HouseInfo> implements HouseInfoService {
    @Autowired
    private PersonInfoMapper personInfoMapper;

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    @Override
    public void add(HouseInfoParam param) {
        LoginUser user = LoginContextHolder.getContext().getUser();
        param.setIsDel(0);
        param.setCreatePerson(user.getName());
        param.setCreateDate(new Date());
        this.baseMapper.insertPlotHouse(param);
        PersonInfoParam personInfoParam = new PersonInfoParam();
        personInfoParam.setIsDel(0);
        personInfoParam.setPersonName(param.getPersonName());
        personInfoParam.setUserName(param.getPhone());
        personInfoParam.setPwd("111111");
        personInfoParam.setPhone(param.getPhone());
        personInfoParam.setCardType(param.getCardType());
        personInfoParam.setCardCode(param.getCardCode());
        personInfoParam.setCreatePerson(user.getName());
        personInfoParam.setCreateDate(new Date());
        personInfoMapper.insertPerson(personInfoParam);
        param.setPersonId(personInfoParam.getId());
        this.baseMapper.insertPersonAndPlotRelation(param);
    }

    @Override
    public void delete(HouseInfoParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(HouseInfoParam param) {
        HouseInfo oldEntity = getOldEntity(param);
        HouseInfo newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public HouseInfoResult findBySpec(HouseInfoParam param) {
        return null;
    }

    @Override
    public List<HouseInfoResult> findListBySpec(HouseInfoParam param) {
        param.setIsDel(0);
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(HouseInfoParam param) {
        Page pageContext = getPageContext();
        param.setIsDel(0);
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<HouseInfoResult> findListBySpecSingle(HouseInfoParam param) {
        param.setIsDel(0);
        return this.baseMapper.customListSingle(param);
    }

    @Override
    public ResponseData listTree(TreeBean param) {
        List<Map<String, String>> listMap = this.baseMapper.queryTree(param);

        if (listMap.size() > 0) {
            List<TreeBean> list = new ArrayList<>();
            for (int i = 0; i < listMap.size(); i++) {
                TreeBean treeBean = new TreeBean();
                treeBean.setXq_name(listMap.get(i).get("xq_name"));
                treeBean.setXq_addr(listMap.get(i).get("xq_addr"));
                treeBean.setXq_phone(listMap.get(i).get("xq_phone"));
                treeBean.setXq_id(String.valueOf(listMap.get(i).get("xq_id")));
                treeBean.setHouse_id(String.valueOf(listMap.get(i).get("house_id")));
                treeBean.setBuild_no(listMap.get(i).get("build_no"));
                treeBean.setUnit_no(listMap.get(i).get("unit_no"));
                treeBean.setLevel_no(listMap.get(i).get("level_no"));
                treeBean.setDoor_no(listMap.get(i).get("door_no"));
                treeBean.setPerson_name(listMap.get(i).get("person_name"));
                treeBean.setPhone(listMap.get(i).get("phone"));
                treeBean.setUser_name(listMap.get(i).get("user_name"));
                list.add(treeBean);
            }

            plotTree tree = new plotTree(plotTree.treeId++ + "", list.get(0).getXq_name());
            //加楼
            for (TreeBean bean : list)
                tree.addHouse(bean);
            //加单元
            for (TreeBean bean : list)
                tree.addUnit(bean);
            //加楼层
            for (TreeBean bean : list)
                tree.addLevel(bean);
            //加户
            for (TreeBean bean : list)
                tree.addDoor(bean);
            //默认开启第一层
            tree.putFirstLevelOpen();
            return ResponseData.success(tree);
        } else {
            return ResponseData.success(400,"无数据",null);
        }
    }

    @Override
    public Integer selectDeleteInfo(Integer param) {
        return this.baseMapper.selectDelete(param);
    }

    @Override
    public void updateDeleteInfo(Integer param) {
        this.baseMapper.updateDelete(param);
    }

    @Override
    public void updatePlotHouseInfo(HouseInfoParam param) {
        this.baseMapper.updatePlotHouse(param);
        this.baseMapper.updatePersonAndPlot(param);
        PersonInfoParam personInfoParam = new PersonInfoParam();
        personInfoParam.setIsDel(0);
        personInfoParam.setPersonName(param.getPersonName());
        personInfoParam.setUserName(param.getPhone());
        personInfoParam.setPhone(param.getPhone());
        personInfoParam.setCardCode(param.getCardCode());
        personInfoParam.setId(param.getPersonId());
        personInfoMapper.updatePerson(personInfoParam);
    }

    private Serializable getKey(HouseInfoParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private HouseInfo getOldEntity(HouseInfoParam param) {
        return this.getById(getKey(param));
    }

    private HouseInfo getEntity(HouseInfoParam param) {
        HouseInfo entity = new HouseInfo();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
